﻿namespace CN2P.Web.Controllers.Api.Proxy
{
    using System.Collections.Generic;
    using System.Web.Http;
    using CN2P.ServiceModel;
    using CN2P.ServiceModel.ServiceClients;

    public class ProductsApiProxyController : BaseApiProxyController
    {
        [AcceptVerbs("GET")]
        public ProductModel Get(string prodId)
        {
            try
            {
                var res = client.ProductsService.GetProduct(prodId);
                return res;
            }
            catch (UnexpectedServiceResponseException exc)
            {
                throw new HttpResponseException(exc.Code);
            }
        }

        [AcceptVerbs("GET")]
        public IDictionary<string,object> GetDetails(string prodId)
        {
            try
            {
                var res = client.ProductsService.GetProductDetails(prodId);
                return res.GetProps();
            }
            catch (UnexpectedServiceResponseException exc)
            {
                throw new HttpResponseException(exc.Code);
            }
        }

        [AcceptVerbs("GET")]
        public ProductModelPagedList GetItems(int pageSize = 10, int pageIndex = 0)
        {
            var res = client.ProductsService.Get(pageIndex, pageSize);
            return res;
        }

        [AcceptVerbs("GET")]
        public ProductModelPagedList GetByTag(string key, int pageSize = 10, int pageIndex = 0)
        {
            var res = client.ProductsService.GetByTag(key, pageIndex, pageSize);
            return res;
        }

        [AcceptVerbs("GET")]
        public ProductModelPagedList GetByTerm(string key, int pageSize = 10, int pageIndex = 0)
        {
            var res = client.ProductsService.GetByName(key, pageIndex, pageSize);
            return res;
        }

        [AcceptVerbs("GET")]
        public IList<string> GetTags()
        {
            var res = client.ProductsService.GetTags();
            return res;
        }
    }
}